<?php
declare(strict_types=1);

namespace traits;

use L;

trait StockTrait
{
    public static function stockStatus($sku_code)
    {
        if (preg_match("/RETURN/i", $sku_code)) {
            return 0;
        } else {
            return 1;
        }
    }

    //把非正常的SKU转为正常的
    public static function tran2sku($sku_code)
    {
        if (preg_match("/RETURN/i", $sku_code)) {
            //把关键字转成大写再来分割
            $sku_code = str_replace("return", "RETURN", $sku_code);
            $skuArr = explode("(RETURN)", $sku_code);
            return trim($skuArr[0]);
        } else {
            $sku_code = preg_replace('/\s+/', '', $sku_code);
            $sku_code = str_replace("（", "(", $sku_code);
            $sku_code = str_replace("(", "@", trim($sku_code));
            if (preg_match("/@/i", $sku_code)) {
                //如果还有()包含的SKU，则再分割
                $skuArr = explode("@", $sku_code);
                return trim($skuArr[0]);
            } else {
                return $sku_code;
            }
        }
    }
    

    public static function stockId($warehouse_id, $sku, $belong)
    {
        $warehouse_id = is_array($warehouse_id) ? implode("','", $warehouse_id) : $warehouse_id;
        $sql = "SELECT stock_id, stock, available_stock, lock_stock, on_way_stock, on_produce FROM stock WHERE 1 
                AND warehouse_id IN('{$warehouse_id}') 
                AND sku = '{$sku}' 
                AND belong = '{$belong}';";
        $result = L::sql($sql, 'warehouse');
        return !empty($result) ? $result[0] : [];
    }


}